package cn.flant.log.adapter;

import cn.flant.log.Logger;

public class LoggerFactory {
	private static final int DEFT = 1;
	private static final int LOG4J = 2;
	private static final int JDK14 = 3;
	private static int logType = 0;

	public static Logger newLoggerAdapter(String name) {
		if (logType > 0) {
			return newLogger(logType, name);
		} else {
			Logger logger = newLog4jAdapter(name);
			if (logger != null) {
				logType = LOG4J;
				return logger;
			}
			logger = newJDK14Adapter(name);
			if (logger != null) {
				logType = JDK14;
				return logger;
			}
			logger = new SimpleLogger(name);
			logType = DEFT;
			return logger;
		}
	}

	private static Logger newLogger(int type, String name) {
		switch (type) {
		case LOG4J:
			return newLog4jAdapter(name);
		case JDK14:
			return newJDK14Adapter(name);
		default:
			return new SimpleLogger(name);
		}
	}

	private static Logger newLog4jAdapter(String name) {
		try {
			return new Log4jAdapter(name);
		} catch (Throwable e) {
			return null;
		}
	}

	private static Logger newJDK14Adapter(String name) {
		try {
			return new JDK14Adapter(name);
		} catch (Throwable e) {
			return null;
		}
	}
}
